{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="row">
        <form class="form-horizontal" method="post">
            <div class="col-sm-12">
                <div class="tabs-container">

                    <div class="tab-content">
                        <div class="ibox-content">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">消息模板：</label>
                                <div class="col-sm-9">
                                    <select class="form-control" name="mbid">
                                        <option value="249538">【那蓝之恋】好友给你发来新消息，请登录那蓝之恋APP及时回复并领取收益,回T退订</option>
                                        <option value="249598">【那蓝之恋】好友给你发来新消息，请登录那蓝之恋APP回复有惊喜哦,回T退订</option>
                                        <option value="249681">【那蓝之恋】好友给你发来新消息，请登录那蓝之恋APP及时回复即可领取10元，回T退订</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">手机号：</label>
                                <div class="col-sm-9">
                                    <textarea rows="10" id="text" placeholder="手动输入多个号码时使用英文逗号分隔每个号码。" class="form-control" name="phone"></textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">快速导入：</label>
                                <div class="col-sm-9">
                                    <div class="btn btn-primary" onclick="txtload()">从txt导入</div>
                                    <div class="btn btn-primary" onclick="excelload()">从excel导入</div>
                                </div>
                            </div>
                            <div style="display:none">
                            <input type="file" name="file" multiple="multiple" accept="text/plain" id="files" onchange="choosefile()" />
                            <input type="file" name="file" multiple="multiple" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/x-excel" id="files2" onchange="choosefile()" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>
{include file="public/footer" /}
{load href="__PLUGINS__iCheck/icheck.min.js"}
{load href="__PLUGINS__iCheck/custom.css"}
{load href="__JS__xlsx.core.min.js"}
<script type="text/javascript">
    $(document).ready(function(){
        $('#files2').change(function (e) {
            var files = e.target.files;
            var fileReader = new FileReader();
            fileReader.onload = function (ev) {
                try {
                    var data = ev.target.result
                    var workbook = XLSX.read(data, {
                        type: 'binary'
                    }) // 以二进制流方式读取得到整份excel表格对象
                    var persons = []; // 存储获取到的数据
                } catch (e) {
                    console.log('文件类型不正确');
                    return;
                }
                // 表格的表格范围，可用于判断表头是否数量是否正确
                var fromTo = '';
                // 遍历每张表读取
                var phone = []
                for (var sheet in workbook.Sheets) {
                    if (workbook.Sheets.hasOwnProperty(sheet)) {
                        fromTo = workbook.Sheets[sheet]['!ref'];
                        console.log(fromTo);
                        console.log(workbook.Sheets[sheet]);
                        for(var i in workbook.Sheets[sheet]){
                            if(workbook.Sheets[sheet][i].v && workbook.Sheets[sheet][i].v.length >= 11){
                                phone.push(workbook.Sheets[sheet][i].v)
                            }
                        }
                        break; // 如果只取第一张表，就取消注释这行
                    }
                }
                console.log(phone)
                const textAreaVal = document.getElementById('text').value;
                var content = phone.join(',')
                content=content.split(/[(\t)\t]+/).join('');
                if(content){
                    document.getElementById('text').value = (textAreaVal ? textAreaVal + ',' : '') + content;
                    layer.msg('本次导入 ' + phone.length + ' 个号码')
                }
            };
            // 以二进制方式打开文件
            fileReader.readAsBinaryString(files[0]);
        });

        $("#button").click(function(){
            ajaxFun("POST","",$(".form-horizontal").serialize(),"json",function(ret){
                if(ret){
                    if(ret.ret){
                        swal_msg(ret.ret,'',"success",1)
                        return false;
                    }else{
                        layer.msg(ret.err)
                    }
                }else{
                    swal_msg("错误",ret,"error");
                }
            });
        });
    });

    function txtload() {
        document.getElementById('files').click();
    }
    function excelload() {
        document.getElementById('files2').click();
    }

    function choosefile () {
        var fileList = document.getElementById('files').files;
        var nameStr = '';
        for (var i = 0; i < fileList.length; i++) {
            nameStr += `${i === 0 ? '' : ', '}${fileList[i].name}`;
            var reader = new FileReader();
            reader.readAsText(fileList[i], "UTF-8");
            reader.onload = function (e) {
                var content = e.target.result;
                console.log(content);
                const textAreaVal = document.getElementById('text').value;
                content=content.split(/[(\r\n)\r\n]+/);
                content.forEach((item,index,array)=>{
                    array[index] = Number(item)
                })
                phone = []
                content.forEach((item,index,array)=>{
                    if(item && !isNaN(item)){
                        phone.push(item)
                    }
                })
                content = phone.join(',')
                if(content){
                    document.getElementById('text').value = (textAreaVal ? textAreaVal + ',' : '') + content;
                    layer.msg('本次导入 ' + phone.length + ' 个号码')
                }

            }
        }
    }

</script>

<style type="text/css">
.tbutton{ 
  position: fixed;
  bottom:0px;
  width: 100%;
  background: #fff;
  padding-top: 20px;
  padding-bottom: 20px;
  border-top: 1px solid #F5EFEF;
  z-index: 99999999999
}
#button{ width:120px; margin: 0 auto;}
.wrapper-content{ padding-bottom:500px;margin-bottom: 75px;}
</style>

<div class="tbutton">
  <div id="button"><button class="btn btn-w-m btn-primary" type="button"><i class="fa fa-check"></i> &nbsp;{present name="name"}修改{else /}提交{/present}</button></div>
</div>
